/**
 * 
 */
package com.losenote.web.control;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.losenote.service.FindNoteService;
import com.losenote.service.UserService;
import com.losenote.service.result.FindNoteResult;
import com.losenote.service.result.UserInfoResult;

/**
 * @author danjingyu
 * 
 */
@Controller
@RequestMapping("/noteDetail.htm")
public class NoteDetailController {

    @Autowired
    private FindNoteService findNoteQueryService;
    @Autowired
    private UserService     userService;

    /**
     * @param httpservletrequest
     * @param modelMap
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView handleRequest(HttpServletRequest httpservletrequest, ModelMap modelMap) {
        ModelAndView view = new ModelAndView("home/noteDetail");
        int noteId = NumberUtils.toInt(httpservletrequest.getParameter("noteId"));
        FindNoteResult result = findNoteQueryService.queryById(noteId);
        modelMap.put("noteResult", result);
        if (result.isSuccess() && result.getFindNote() != null) {
            UserInfoResult userResult = userService.queryById(NumberUtils.toInt(result
                .getFindNote().getCreator()));
            modelMap.put("userResult", userResult);
        }
        return view;
    }

    public void setFindNoteQueryService(FindNoteService findNoteQueryService) {
        this.findNoteQueryService = findNoteQueryService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

}
